﻿using UnityEngine;
using UnityEditor;
using System;

namespace GameBasic.Editors
{
    [CustomPropertyDrawer(typeof(EnumDataAttribute))]
    public class EnumDataAttributeDrawer : PropertyDrawer
    {
        const float Gap = 4;

        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (attribute is EnumDataAttribute att)
            {
                SerializedProperty pStat = property.FindPropertyRelative(att.typeProperty ?? "x");
                SerializedProperty pValue = property.FindPropertyRelative(att.valueProperty ?? "y");

                if (pStat != null && pValue != null)
                {
                    // Custom enum?
                    position.width = (position.width - Gap) * 0.5f;
                    if (att.enumType != null)
                        pStat.intValue = EnumDrawerUtil.Draw(position, att.enumType, pStat.intValue);
                    else
                        EditorGUI.PropertyField(position, pStat, GUIContent.none, false);

                    position.x += position.width + Gap;
                    EditorGUI.PropertyField(position, pValue, GUIContent.none, false);
                }
                else if (att.enumType != null)
                {
                    property.intValue = EnumDrawerUtil.Draw(position, att.enumType, property.intValue);
                }
            }
        }
    }
}